package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.OrgRepository;
import com.hualude.roadsign.repository.PushHistoryRepository;
import com.hualude.roadsign.repository.SignalLampFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.SignalLampCheckService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ding on 2017/8/4.
 */
@Service
public class SignalLampCheckServiceImpl implements SignalLampCheckService {

    @Resource
    private SignalLampFaultRepository signalLampFaultRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private OrgRepository orgRepository;
    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DTResponseDto getSignalLampFaultCheck4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception {
        List<DTLampFaultDto> dto4DTs = signalLampFaultRepository.getSignalLampFaultCheck4DT(dtRequestDto, actorId, orgId);
        Integer count = signalLampFaultRepository.getSignalLampFaultCheckCount4DT(dtRequestDto, actorId, orgId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for(DTLampFaultDto dtMachineFaultDto :dto4DTs ){
            String creatorName = userRepository.getUserNameByUserId(dtMachineFaultDto.getLampCreatorId());
            dtMachineFaultDto.setLampCreatorName(creatorName);
            String checkerName = userRepository.getUserNameByUserId(dtMachineFaultDto.getLampCheckerId());
            dtMachineFaultDto.setLampCheckerName(checkerName);
            if(dtMachineFaultDto.getLampMainCheckerId()!=null){
                String mainCheckerName = userRepository.getUserNameByUserId(dtMachineFaultDto.getLampMainCheckerId());
                dtMachineFaultDto.setLampMainCheckerName(mainCheckerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer lampFaultAudit(Integer isPass, String feedback, Integer lampFaultId, Integer userId) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        Integer check = signalLampFaultRepository.getLampCheck(lampFaultId);
        //check:1,主管在审核
        check = (isPass == 2) ? 1 : check;
        if(check==1){
            if(isPass==1|| isPass == 2){
                final String[] strFaultType = {"损毁", "其他"};
                final SignalLampFaultDto faultDto = signalLampFaultRepository.getInfoByFaultId(lampFaultId);
                final Integer orgId = faultDto.getLampOrgId();
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
                final String msg = "信号灯 NO：" + faultDto.getSignalLampId() + " " +
                        strFaultType[faultDto.getLampType()];
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),3);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                if (isPass == 1)
                    return signalLampFaultRepository.faultAudit2Success(isPass,feedback,lampFaultId,userId,dateNowStr);
                else
                    return signalLampFaultRepository.straightFaultAudit(isPass, feedback, lampFaultId, userId, dateNowStr);

            }
            else{
                return signalLampFaultRepository.faultAudit2Fail(isPass,feedback,lampFaultId,userId,dateNowStr);
            }
        }
        else {
            if(isPass==1){
                final SignalLampFaultDto faultDto = signalLampFaultRepository.getInfoByFaultId(lampFaultId);
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(faultDto.getLampMainOrgId(), 7);
                final String msg = "信号灯 NO：" + faultDto.getSignalLampId() + " 待审核";
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(faultDto.getLampMainOrgId(), 7, msg, formatter.format(new Date()),2);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
            return signalLampFaultRepository.faultAudit(isPass,feedback,lampFaultId,userId,dateNowStr);
        }
    }

    public List<SignalLampFaultDto> getLampFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception {
        return signalLampFaultRepository.getLampFaultCheckList4App(orgId, actorIds, faultStatus, count, size);
    }

    public SignalLampFaultDto getSignalLampInfo2Fault(Integer signalLampId) throws Exception {
        SignalLampFaultDto signalLampFaultDto = signalLampFaultRepository.getSignalLampInfo2Fault(signalLampId);
        signalLampFaultDto.setSignalLampCreatorName(userRepository.getUserNameByUserId(signalLampFaultDto.getSignalLampCreatorId()));
        signalLampFaultDto.setSignalLampOrgName(orgRepository.getMainOrgName(signalLampFaultDto.getSignalLampOrgId()));
        signalLampFaultDto.setSignalLampMainOrgName(orgRepository.getMainOrgName(signalLampFaultDto.getSignalLampMainOrgId()));
        signalLampFaultDto.setSignalLampId(signalLampId);
        return signalLampFaultDto;
    }

    public SignalLampFaultDto getSignalLampFaultInfo2Fault(Integer signalLampId, Integer lampFaultId) throws Exception {
        SignalLampFaultDto signalLampFaultDto = signalLampFaultRepository.getSignalLampInfo2Fault(signalLampId);
        SignalLampFaultDto machineFault = signalLampFaultRepository.getSignalLampFaultInfo2Fault(lampFaultId);
        machineFault.setSignalLampProvince(signalLampFaultDto.getSignalLampProvince());
        machineFault.setSignalLampCity(signalLampFaultDto.getSignalLampCity());
        machineFault.setSignalLampArea(signalLampFaultDto.getSignalLampArea());
        machineFault.setSignalLampStartRoad(signalLampFaultDto.getSignalLampStartRoad());
        machineFault.setSignalLampEndRoad(signalLampFaultDto.getSignalLampEndRoad());
        machineFault.setSignalLampLongitude(signalLampFaultDto.getSignalLampLongitude());
        machineFault.setSignalLampLatitude(signalLampFaultDto.getSignalLampLatitude());
        machineFault.setLampOrgId(signalLampFaultDto.getSignalLampOrgId());
        machineFault.setLampMainOrgId(signalLampFaultDto.getSignalLampMainOrgId());
        UserDto userDto = userRepository.getUserById(machineFault.getLampCreatorId());
        machineFault.setLampCreatorName(userDto.getUserName());
        machineFault.setLampCreatorPhone(userDto.getUserMobile());
        return machineFault;
    }
}
